<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户编号" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户编号"></el-input>
    </el-form-item>
    <el-form-item label="商品编号" prop="productId">
      <el-input v-model="dataForm.productId" placeholder="商品编号"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="productName">
      <el-input v-model="dataForm.productName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="购买数量" prop="productCount">
      <el-input v-model="dataForm.productCount" placeholder="购买数量"></el-input>
    </el-form-item>
    <el-form-item label="商品单价" prop="productPrice">
      <el-input v-model="dataForm.productPrice" placeholder="商品单价"></el-input>
    </el-form-item>
    <el-form-item label="订单状态：0未支付、1已支付、2已发货、3已收货、4已完成" prop="status">
      <el-input v-model="dataForm.status" placeholder="订单状态：0未支付、1已支付、2已发货、3已收货、4已完成"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="craeteTime">
      <el-input v-model="dataForm.craeteTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="支付时间" prop="payTime">
      <el-input v-model="dataForm.payTime" placeholder="支付时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userId: '',
          productId: '',
          productName: '',
          productCount: '',
          productPrice: '',
          status: '',
          craeteTime: '',
          payTime: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '用户编号不能为空', trigger: 'blur' }
          ],
          productId: [
            { required: true, message: '商品编号不能为空', trigger: 'blur' }
          ],
          productName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          productCount: [
            { required: true, message: '购买数量不能为空', trigger: 'blur' }
          ],
          productPrice: [
            { required: true, message: '商品单价不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '订单状态：0未支付、1已支付、2已发货、3已收货、4已完成不能为空', trigger: 'blur' }
          ],
          craeteTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          payTime: [
            { required: true, message: '支付时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/seckill/porder/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.porder.userId
                this.dataForm.productId = data.porder.productId
                this.dataForm.productName = data.porder.productName
                this.dataForm.productCount = data.porder.productCount
                this.dataForm.productPrice = data.porder.productPrice
                this.dataForm.status = data.porder.status
                this.dataForm.craeteTime = data.porder.craeteTime
                this.dataForm.payTime = data.porder.payTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/seckill/porder/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userId': this.dataForm.userId,
                'productId': this.dataForm.productId,
                'productName': this.dataForm.productName,
                'productCount': this.dataForm.productCount,
                'productPrice': this.dataForm.productPrice,
                'status': this.dataForm.status,
                'craeteTime': this.dataForm.craeteTime,
                'payTime': this.dataForm.payTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
